#include "..\include\Types.h"

/*
"Patch in C++" template by UltraShot
Brightness Controller Patch by mc_kibel
*/

extern __thumb DISPLAY_SetBrightness(int display, int brightness);
extern __thumb Timer_Set(int time, void (*onTimer)(u16 timerID,  void *),  void *);
extern __thumb Timer_ReSet(u16 * timer ,int time, void (*onTimer)(u16 timerID, void *), void *);
extern __thumb REQUEST_DATEANDTIME_GET(const int * __zero , DATETIME *dt);
extern __thumb int GetBattaryState(const int * , BATT *);
extern __thumb returnto();

u16 timer;
unsigned int val;
int hour;
unsigned int RemainingCapacityInPercent;
unsigned int r=0;


__root int GetTime (void)
{
  DATETIME Date_and_time;
  REQUEST_DATEANDTIME_GET (0, &Date_and_time); 
  hour = Date_and_time.time.hour; 
  return (hour);
}

__root int GetBatteryLevel (void)
{
  BATT Battery;
  GetBattaryState((int const*)1 , &Battery);
  RemainingCapacityInPercent = Battery.RemainingCapacityInPercent;
  return (RemainingCapacityInPercent);
}

__root void InitInteligenceAutoBacklight (void)
{
  GetTime();
  GetBatteryLevel();
  
  if ( RemainingCapacityInPercent<=10 )
  {
    val = 10;
  }
  else
  {  
    if ( hour>=0 && hour<8 )
      val = 10;
    else if ( hour >= 8 && hour < 12 ) 
      val = 30;
    else if ( hour >= 12 && hour < 20 ) 
      val = 60;
    else if ( hour >= 20 && hour > 0 ) 
      val = 10;
  }
  
  DISPLAY_SetBrightness(0, (val));
}


__root void onTimer (u16 unk , void * data)
{
  {
    InitInteligenceAutoBacklight();
    Timer_ReSet(&timer,3000,onTimer,0);
  }
}


__root void fixed_function(void)
{ 
  if (r==0)
  {
      timer=Timer_Set(3000,onTimer,0);
      r++;
      
  }
  returnto();
};

